<?php
// +----------------------------------------------------------------------
// | 幕小铺
// +----------------------------------------------------------------------
// | 版权所有 2016~2020 幕小铺科技（大连）有限公司
// +----------------------------------------------------------------------
// | 2020/03/14
// +----------------------------------------------------------------------

namespace app\user_center\admin;

use app\admin\controller\Admin;
use app\common\builder\ZBuilder;

/**
 * 申请团长 控制器
 * @package app\user\admin
 */
class TeamLeader extends Admin
{
	
    /**
     * 申请列表
     * @author shiqiang <976162041@qq.com>
     * @return mixed
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
	public function index(){
		cookie('__forward__', $_SERVER['REQUEST_URI']);
		
		// 获取排序
        $order = $this->getOrder();
        // 获取筛选
        $map = $this->getMap();
		
		$data = input();
		//tab
        $list_tab = [
            'tab1' => ['title' => '待审核', 'url' => url('index', ['group' => 'tab1'])],
            'tab2' => ['title' => '已通过', 'url' => url('index', ['group' => 'tab2'])],
            'tab3' => ['title' => '已屏蔽', 'url' => url('index', ['group' => 'tab3'])],
        ];

        // 数据列表
        $group = isset($data['group']) ? $data['group'] : 'tab1';
        switch ($group) {
            case 'tab1':
                $map[] = ['tl_status', '=', 0];
                break;
            case 'tab2':
                $map[] = ['tl_status', '=', 1];
                break;
            case 'tab3':
                $map[] = ['tl_status', '=', 2];
                break;
        }
		
		// 数据列表
        $data_list = model('UcApplyTeamLeader', 'model\user') -> where($map) -> order($order) -> paginate();
		
		$rel_list = [];
		foreach($data_list as $key=> $value){
			$rel_list[$key] = $value;
			$rel_list[$key]['nick_name'] = $value['get_uc_member']['nick_name'];
		}
		// 分页数据
        $page = $data_list->render();
		
		// 详情
        $btn_details = [
            'title' => '详情',
            'icon'  => 'fa fa-fw fa-bookmark',
            'href'  => url('details', ['id' => '__id__'])
        ];        // 通过
        $btn_yes = [
            'title' => '通过',
            'icon'  => 'fa fa-fw fa-bookmark',
            'class' => 'btn btn-xs btn-default ajax-get confirm',
            'href'  => url('status_yes', ['ids' => '__id__'])
        ];

        // 屏蔽
        $btn_no = [
            'title' => '屏蔽',
            'icon'  => 'fa fa-fw fa-bookmark',
            'class' => 'btn btn-xs btn-default ajax-get',
            'href'  => url('status_no', ['ids' => '__id__'])
        ];
		
		// 批量审核
        $btn_status_yes = [
            'title' => '批量通过',
            'icon'  => 'fa fa-fw fa-bookmark',
            'class' => 'btn btn-success ajax-post confirm',
            'href'  => url('status_yes')
        ];
        // 批量屏蔽
        $btn_status_no = [
            'title' => '批量屏蔽',
            'icon'  => 'fa fa-fw fa-bookmark',
            'class' => 'btn btn-warning  js-get',
            'href'  => url('status_no')
        ];
		
		// 使用ZBuilder快速创建数据表格
        return ZBuilder::make('table')
            ->setPageTitle('团长申请列表') // 设置页面标题
            ->setTableName('uc_apply_team_leader') // 设置数据表名
			->setSearch(['user_id' => '会员UID']) // 设置搜索参数
            ->addColumns([
                ['user_id', 'UID'],
                ['nick_name', '昵称', 'link', url('user_center/index/details', ['uid' => '__user_id__']), '_blank', 'pop'],
				['username', '姓名'],
				['mobile', '手机号'],
				['city', '所在城市'],
				['attributes', '个人属性'],
				['address', '自提点地址'],
				['experience', '团长经验'],
				['wechat_group', '是否有周边微信群'],
				['wechat_num', '群内人数'],
                ['create_time', '创建时间', 'datetime'],
                ['right_button', '操作', 'btn']
            ])
            ->addFilter(['nick_name']) // 添加筛选
			->setTabNav($list_tab,  $group)
            ->addTimeFilter('create_time', '', ['开始时间', '结束时间'])
			->addTopButton('custom', $btn_status_yes) // 顶部 批量审核 通过
            ->addTopButton('custom', $btn_status_no, true) // 顶部 批量s审核 屏蔽
			// ->addRightButton('custom', $btn_details, true) // 添加授权按钮
			->addRightButton('btn_yes', $btn_yes) // 添加授权按钮
			->addRightButton('btn_no', $btn_no, true) // 添加授权按钮
			->replaceRightButton(['tl_status' => 1], '', ['btn_yes', 'btn_no']) // 修改id为1的按钮
			->replaceRightButton(['tl_status' => 2], '', ['btn_yes', 'btn_no']) // 修改id为1的按钮
			->setPages($page) // 设置	分页数据
            ->setRowList($rel_list) // 设置表格数据
            ->fetch(); // 渲染页面
	}
	
	/**
     * 详情
     * @author shiqiang
     * @return mixed
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function details($id = ''){
		$info = model('UcApplyTeamLeader', 'model\user') -> where('id', $id) -> find();

        //批量审核最多允许20个
		return ZBuilder::make('form')
            ->setPageTitle('申请详情') // 设置页面标题
            ->addFormItems([ // 批量添加表单项
                ['text', 'username', '姓名', '', '', '', 'readonly="readonlys"'],
                ['text', 'mobile', '手机号', '', '', '', 'readonly="readonlys"'],
                ['text', 'city', '所在城市', '', '', '', 'readonly="readonlys"'],
                ['text', 'attributes', '个人属性', '', '', '', 'readonly="readonlys"'],
                ['text', 'address', '自提点地址', '', '', '', 'readonly="readonlys"'],
                ['text', 'experience', '团长经验', '', '', '', 'readonly="readonlys"'],
                ['text', 'wechat_group', '是否有周边微信群', '', '', '', 'readonly="readonlys"'],
                ['text', 'wechat_num', '群内人数', '', '', '', 'readonly="readonlys"'],
            ])
			->hideBtn(['submit', 'back'])
			->setFormData($info) // 设置表单数据
            ->fetch();
    }
	
	/**
     * 人工审核 批量通过
     * @author shiqiang
     * @return mixed
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function status_yes($ids = []){
        //批量审核最多允许20个
        $list = model('UcApplyTeamLeader', 'model\user') -> where('id', 'in', $ids) -> order('create_time desc') ->limit(20) -> column('id');//dump($list);die;
		$rel_list = [];
        foreach ($list as $key => $value) {
            $rel_list[] = [
                'id'  		=> $value,
                'tl_status' => 1,
            ];
        }
		$result = model('UcApplyTeamLeader', 'model\user') -> useData($rel_list);
        if($result['code'] == 200){
            $this -> success('操作成功');
        }else{
            $this -> error('操作失败');
        }
    }

    /**
     * 人工审核 批量屏蔽
     * @author shiqiang
     * @return mixed
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function status_no($ids = []){
        //批量审核最多允许20个
		if ($this->request->isPost()) {
			$data = $this->request->post('', null, 'trim');
			$reason = $data['reason'];
			
			if($reason == ''){
				$this -> error('数据格式错误');
			}
			
			$list = model('UcApplyTeamLeader', 'model\user') -> where('id', 'in', $ids) -> order('create_time desc') ->limit(20) -> column('id');
			$rel_list = [];
			foreach ($list as $key => $value) {
				$rel_list[] = [
					'id'  		=> $value,
					'tl_status' => 2,
					'reason'  	=> $reason,
				];
			}
			$result = model('UcApplyTeamLeader', 'model\user') -> useData($rel_list);
			if($result['code'] == 200){
				$this->success('屏蔽成功', null, '_parent_reload');
			}else{
				$this -> error('操作失败');
			}
		}
		
		return ZBuilder::make('form')
            ->setPageTitle('拒绝认证') // 设置页面标题
            ->addFormItems([ // 批量添加表单项
                ['textarea', 'reason', '拒绝理由'],
            ])
            ->setFormData() // 设置表单数据
            ->fetch();
    }
	
}
